﻿using EquipmentAlarmSystem.Models;
using EquipmentAlarmSystem.Service.ContentPushUnit;
using EquipmentAlarmSystem.Service.Library;
using EquipmentAlarmSystem.Service.Models;
using EquipmentAlarmSystem.Service.Models.Enum;
using EquipmentAlarmSystem.Service.OperationManagementUnit;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;

namespace EquipmentAlarmSystem.Controllers;

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Journal()
    {
        LogModel logModel = new() { Loging = Logging.CacheLogs };
        return View(logModel);
    }

    [Authorize]
    public IActionResult AlarmTesting([FromForm] SendingMessages sendingMessages)
    {
        //构造对象
        AlarmTestingModel alarmTestingModel = new()
        {
            AlarmAddress = SmsAlarmConnection.AlarmAddress,
            AlarmStatus = SmsAlarmConnection.AlarmStatus,
            IsServerRun = SmsAlarmConnection.IsServerRun,
            NextHeartbeatTimestamp = SmsAlarmConnection.NextHeartbeatTimestamp,
            CurrentTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds()
        };

        if (sendingMessages.To is null)
        {
            return View(alarmTestingModel);
        }
        if (sendingMessages.MsgType == MessageType.Sms)
        {
            InformationPushService.Add(MessageType.Sms, new List<string>() { sendingMessages.To }, sendingMessages.Content);
        }
        if (sendingMessages.MsgType == MessageType.Call)
        {
            InformationPushService.Add(MessageType.Call, new List<string>() { sendingMessages.To }, sendingMessages.Content);
        }
        alarmTestingModel.Content = "任务已添加";
        return View(alarmTestingModel);

    }

    public IActionResult RunningEquipment()
    {
        RunningEquipmentModel running = new();

        running.List = new();

        foreach (var item in Management.RunningServicesId)
        {
            //配置文件序列化
            DeviceInformation deviceInformation = JsonSerializer.Deserialize<DeviceInformation>(Basics.GetEquipmentConfiguration(item));

            running.List.Add(new()
            {
                Id = item,
                Name = deviceInformation.Name
            });
        }
        return View(running);
    }
}
